<?php
date_default_timezone_set('UTC');
/**
 * @var BookedController $this
 * @var Booked $item
 */
$this->breadcrumbs = array(
    'Booked Management',
);
?>


<div class="page-header position-relative">
    <h1> Booked Management</h1>
</div><!--/.page-header-->
<div class="row-fluid">
    <div class="span4 widget-container-span ui-sortable">
        <div class="widget-box collapsed">
            <div class="widget-header widget-header-small header-color-blue">
                <h6>
                    Excel Export
                </h6>

                <div class="widget-toolbar">

                    <a href="#" data-action="collapse">
                        <i class="icon-chevron-down"></i>
                    </a>
                </div>
            </div>

            <div class="widget-body">
                <div class="widget-body-inner" style="display: none;">
                <div class="widget-main">
                    <div class="form-export">
                        <?php
                        $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                            'name' => 'publishDate',
                            'value' => date('Y-m-d'),
                            'options' => array(
                                'showAnim' => 'fold',
                                'changeMonth' => true,
                                'changeYear' => true,
                                'dateFormat' => 'yy-mm-dd',
                                'id' => 'publishDate'
                            ),
                        ));
                        ?>
                        <button id="buttonExport" href="#" class="btn btn-small btn-success">Export to excel</button>
                    </div>
                </div>
                </div>
                </div>
        </div>
    </div>
</div>
<div class="row-fluid">
    <?php
    if(Yii::app()->user->hasFlash('error')) {
        ?>
        <div class="alert alert-block">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <h4>Warning!</h4>
            <?php echo Yii::app()->user->getFlash('error')?>
        </div>
        <?php
    }
    ?>
    <div>
    <div class="pull-right">
        <div class="widget-toolbar">                
            <div class="today">
                <form action="<?php echo Yii::app()->createUrl('bookingsystem/booked') ?>" method="POST" id="date_time_book" style="margin:0;">
                    <div class="row-fluid input-append">
                        <input type="text" value="<?php if (isset($start_date)) {
                            echo $start_date;
                        } else {
                            echo date('d-m-Y');
                        } ?>" id="Event_date" name="Booked[date]" class="date-picker" readonly="readonly" data-date-format="dd-mm-yyyy">                                       <span class="add-on">
                        <i class="icon-calendar"></i>
                    </span>
                    <span class="help-inline"><div style="display:none" id="Event_expired_date_em_" class="errorMessage"></div></span>
                    </div>                    
                </form>
            </div>
        </div>
    </div>
</div>
    <div class="table-header">
        Booked Management
    </div>
    
    <table id="sample-table-2" class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th width="20"></th>
                <th>Email</th>
                <th width="150">Event</th>
                <th width="180">Ticket Order</th>
                <th>Ticket Code</th>
                <th width="50">Payment Type</th>
                <th>Status</th>
                <th>Payment</th>
                <th>Book date</th>
                <th width="20"></th>
            </tr>
        </thead>

        <tbody>
            <?php
            if (count($data) > 0):
                foreach ($data as $k => $item) :
                    ?>
                    <tr>
                        <td class="center">
                            <label>
                                <?php echo $k + 1; ?>
                            </label>
                        </td>
                        <td class="span1">
                            <?php echo $item->user->email ?>
                        </td>
                        <td>
                            <?php $result = MyFunctionCustom::getInfoEvent($item->event_id, 2); ?>
                            <?php if ($result == null) : ?>
                            <span class="label label-important label-mini" title="This event has been deleted by user">Deleted</span>
                            <?php else : ?>
                            <?php echo $result; ?>
                            <?php endif; ?>
                        </td>
                        <td>
                            <a class="blue views-booked-details" href="#modal-form" role="button" data-toggle="modal" rel="<?php echo $item->id; ?>">
                                <b>
                                    <?php echo $item->ticket_order_number; ?>
                                </b>
                            </a>
                        </td>
                        <td>
                            <ul class="items-table">
                                <?php foreach ($item->booked_details as $key => $value) : ?>
                                <?php $ticket_model = Ticket::model()->findByPk($value->ticket_id); ?>
                                <li><?php echo $ticket_model->ticket_code ?></li>
                                <?php endforeach; ?>
                            </ul>
                        </td>
                        <td>
                            <b class="green"><?php echo MyFunctionCustom::getPayment($item->payment_type); ?></b>
                        </td>
                        <td>
                            <?php
                            $stt = MyFunctionCustom::getStatusBooking($item->status);
                            ?>
                            <?php if ($item->status == STATUS_PENDING) : ?>
                            <a class="green" href="<?php echo Yii::app()->createAbsoluteUrl($parentUrl . '/update', array('id' => $item->id)); ?>">
                                <span class="label label-important label-mini" title="Change status"><?php echo $stt; ?></span>
                            </a>
                            <?php elseif ($item->status == STATUS_DELIVER): ?>
                            <a class="green" href="<?php echo Yii::app()->createAbsoluteUrl($parentUrl . '/update', array('id' => $item->id)); ?>">
                                <span class="label label-info label-mini" title="Change status"><?php echo $stt; ?></span>
                            </a>
                            <?php else: ?>
                            <span class="label label-success label-mini" title="Finished"><?php echo $stt; ?></span>
                            <?php endif; ?>
                        </td>
                        <td><b class="<?php echo $item->payment_status ? 'green' : 'red'; ?>">
                            <?php
                            echo $item->payment_status ? 'Yes' : 'No';
                            ?>
                        </b></td>
                        <td>
                            <?php echo date('h:i / d-M-Y', strtotime($item->booked_time)); ?>
                        </td>
                        <td class="td-actions">
                            <div class="hidden-phone visible-desktop action-buttons">

                                <?php if ($item->status != STATUS_FINISH && $item->status != STATUS_DELIVER) : ?>
                                <?php
                                echo CHtml::link(
                                    '<i class="icon-trash bigger-130"></i>',
                                    array('booked/delete', 'id' => $item->id),
                                    array('confirm' => 'Are you sure you want to delete this item?', 'class' => 'red')
                                );
                                ?>
                                <?php endif; ?>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach;endif; ?>
        </tbody>
    </table>
</div>
<div id="modal-form" class="modal hide" tabindex="-1">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="blue bigger">Views Booked Details</h4>
    </div>

    <div class="modal-body overflow-visible">

    </div>
    <div class="modal-footer">
        <button class="btn btn-small" data-dismiss="modal">
            <i class="icon-remove"></i>
            Close
        </button>
    </div>
</div>
<script type="text/javascript">
$(document).ready(function(){
     $('#Event_date').change(function(){
        $(this).parent().parent('form').submit();
    }); 
});
    $('.views-booked-details').on('click', function () {
        var booked_id = $(this).attr('rel');
        $.ajax({
            url:'<?php echo Yii::app()->createAbsoluteUrl("bookingsystem/booked/view") ?>',
            data:{id:booked_id},
            async:true,
            beforeSend:function () {
                $('.modal-body').empty();
                $('.modal-body').append('<div class="loading"><div style="width:134px;height:100px;margin:0 auto;padding-top: 10px;"><img src="<?php echo Yii::app()->theme->baseUrl; ?>/frontend/images/ajaxloader.gif"></div></div>');
            },
            success:function (data) {

                $('.modal-body').html(data);
            }
        });
    });
$(function () {
    $('#buttonExport').click(function () {
        date = $('#publishDate').val();
        window.location.href='<?php echo $this->createUrl('exportExcel') ?>?date='+date;
        return false;
    });
})

</script>
<style>
    span.form-export{
        margin-right: 5px;
    }
    span.form-export input[type="text"]{
        margin-top: 4px;
    }
    #buttonExport{
        margin-top: -6px;
    }
</style>
<style>
    .widget-toolbar{
        line-height: 18px !important;
    }
    .today form {
        margin-top: 4px !important;
    }

    .today input{
        width: 100px;
    }
</style>